package com.android.monee.binding.binder;

import com.android.monee.binding.Field;
import com.android.monee.binding.observable.Observable;
import com.android.monee.binding.observer.ObserverImpl;

public class Simple1WBinder<S,
								  So extends Observable<S>, 
                                  Sf extends Field<S, V>,
                                  T,
                                  Tf extends Field<T, V>,
                                  V> {

	private ObserverImpl<S, T, V> observer;

	public Simple1WBinder(So observableSource, Sf sourceField, T target,
			Tf targetField) {
		construct(observableSource, sourceField, target, targetField);
		
		observableSource.notify(sourceField.getClass());
	}
	
	public Simple1WBinder(So observableSource, Sf sourceField, T target,
			Tf targetField, Boolean notify) {
		construct(observableSource, sourceField, target, targetField);
		
		if(notify)
			observableSource.notify(sourceField.getClass());
	}

	private void construct (So observableSource, Sf sourceField, T target,
			Tf targetField){
		final Simple1WBinder<S, So, Sf, T, Tf, V> that = this; 
		this.observer = new ObserverImpl<S, T, V>(sourceField, target,
				targetField) {
			@Override
			public void updateTarget(T target, V newVal) {
				that.updateTarget(target, newVal);
			}
		};
		observableSource.addObserver(observer);
	}
	
	public void updateTarget(T target, V value) {
		defaultUpdateTraget(target, value);
	}
	
	public final void defaultUpdateTraget(T target, V value){
		observer.defaultUpdateTarget(target, value);
	}
}
